package com.zmn.oms.services.impl.product;

import com.vdurmont.emoji.EmojiParser;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.persistence.interfaces.product.OrderProductDao;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import io.shardingsphere.api.HintManager;
import io.shardingsphere.core.keygen.DefaultKeyGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 类描述：订单详情
 *
 * @author liuying
 * @date 2018/11/06 11:41
 */
@Service
@Slf4j
public class OrderProductServiceImpl implements OrderProductService {

    @Resource
    private OrderProductDao orderProductDao;
    @Autowired
    private DefaultKeyGenerator defaultKeyGenerator;

    @Override
    public List<OrderProduct> listOrderProductByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderProductDao.listOrderProductByOrderIdList(orderIdList);
        }
    }

    @Override
    public List<OrderProduct> listOrderProductByOrderId(Long orderId) {
        return orderProductDao.listByOrderId(orderId);
    }

    @Override
    public List<OrderProduct> listOrderProductByOrderIdSrcMaster(Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderProductDao.listByOrderId(orderId);
        }
    }

    @Override
    public void insertOrderProductList(List<OrderProduct> orderProductList) {
        Date now = DateUtil.getNow();
        orderProductList.forEach((item)->{
            item.setProId(defaultKeyGenerator.generateKey().longValue());

            if (StringUtil.isNotBlank(item.getFault())) {
                item.setFault(EmojiParser.parseToUnicode(item.getFault()));
            }
            Date createTime = Optional.ofNullable(item.getCreateTime()).orElse(now);
            item.setCreateTime(createTime);
            item.setUpdateTime(createTime);

            // log.info("insertOrderProductList orderId={}，proId={}", item.getOrderId(), item.getProId());
            orderProductDao.insertOrderProduct(item);
        });
    }

    @Override
    public Integer deleteOrderProductByOrderId(Long orderId) {
        return orderProductDao.deleteByOrderId(orderId);
    }

    @Override
    public void updateProductIdByKey(OrderProduct orderProduct) {

        if (StringUtil.isNotBlank(orderProduct.getFault())) {
            orderProduct.setFault(EmojiParser.parseToUnicode(orderProduct.getFault()));
        }

        orderProductDao.updateProductIdByKey(orderProduct);
    }

    @Override
    public void updateBrandByKey(OrderProduct orderProduct) {
        orderProductDao.updateBrandByKey(orderProduct);
    }

    @Override
    public void updateTariffIdByKey(OrderProduct orderProduct) {
        orderProductDao.updateTariffIdByKey(orderProduct);
    }
}
